<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2020/07/27
 * Time: 下午5:13
 */
namespace App\Api\Controllers\Lianhou;

use App\Api\Controllers\BaseController;
use App\Common\PaySuccessAction;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use App\Api\Controllers\Cmbc\BaseController as base;

class NotifyController extends BaseController{

    public function pay_notify_url(Request $request)
    {
        try {
            $data = $request->all();

            Log::channel("lianhou")->info("qr_submit_ret", $data);
            $config = DB::table('lianhou_configs')->where('platform_id', $data['agentCode'])->first();

            $base = \App\Api\Controllers\Lianhou\BaseController::getInstance();
            $ret  =  json_decode($base->decryptStr($data['reqData'], $config->key), true);
            if ($ret) {

                if (isset($ret['payStatus']) && $ret['payStatus'] == 'SUCCESS') {

                    if (isset($ret['outTradeNo'])) {
                        $out_trade_no = $ret['outTradeNo']; //商户订单号（原交易流水）
                        $trade_no = $ret['orderNo']; //系统订单号

                        $day = date('Ymd', time());
                        $table = 'orders_' . $day;
                        if (Schema::hasTable($table)) {
                            $order = DB::table($table)->where('out_trade_no', $out_trade_no)->first();
                        } else {
                            $order = Order::where('out_trade_no', $out_trade_no)->first();
                        }

                        //订单存在
                        if ($order) {
                            //系统订单未成功
                            if ($order->pay_status == '2') {

                                $pay_time = $ret['payTime'];
                                $buyer_id = $ret['openId'];
                                $buyer_pay_amount = $ret['payAmount'];
                                $in_data = [
                                    'status' => '1',
                                    'pay_status' => 1,
                                    'pay_status_desc' => '支付成功',
                                    'trade_no' => $trade_no,
                                    'pay_time' => $pay_time,
                                    'buyer_id' => $buyer_id,
                                    'buyer_pay_amount' => $buyer_pay_amount,
                                ];

                                $this->update_day_order($in_data, $out_trade_no);

                                if (strpos($out_trade_no, 'scan')) {

                                } else {
                                    //支付成功后的动作
                                    $data = [
                                        'ways_type' => $order->ways_type,
                                        'company' => $order->company,
                                        'ways_type_desc' => $order->ways_type_desc,
                                        'source_type' => '80001', //返佣来源
                                        'source_desc' => '联厚支付', //返佣来源说明
                                        'total_amount' => $order->total_amount,
                                        'out_trade_no' => $order->out_trade_no,
                                        'other_no' => $order->other_no,
                                        'rate' => $order->rate,
                                        'fee_amount' => $order->fee_amount,
                                        'merchant_id' => $order->merchant_id,
                                        'store_id' => $order->store_id,
                                        'user_id' => $order->user_id,
                                        'config_id' => $order->config_id,
                                        'store_name' => $order->store_name,
                                        'ways_source' => $order->ways_source,
                                        'pay_time' => $pay_time,
                                        'device_id' => $order->device_id,
                                    ];
                                    PaySuccessAction::action($data);
                                }
                            }
                        }
                    }
                    echo '{"code":"0000","message":"成功"}';
                }
            }
        } catch (\Exception $ex) {

        }
    }


    

}
